/**
 * 
 */
package com.hk.web.controller.pc;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.kyc.HKNewsReq;
import com.hk.Mgr.web.message.kyc.HKNewsRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.DateUtil;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.StringHelper;
import com.hk.commons.entity.HKNews;
import com.hk.service.kyc.INews;

/**
 * @author psx 产品信息实现类
 */
@Controller
@RequestMapping(value = BaseCtrl.PC + "/newsCtrl")
public class PcNewsCtrl extends BaseCtrl {

	@Autowired
	private INews newsService;
	
	/**
	 * 获取新闻列表
	 * 
	 * @author jeff
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<List<HKNewsRes>> list(@RequestBody HKNewsReq req) {
		
		
		ResponseBase<List<HKNewsRes>> resp = new ResponseBase<List<HKNewsRes>>();
		List<HKNewsRes> newsReslist = new ArrayList<HKNewsRes>();
		List<HKNews> newslist = new ArrayList<HKNews>();
		HqlFilter hqlFilter = new HqlFilter();

		logger.info("get news list type={}, channel={}", req.getType(), req.getChannel());
		
		if(StringHelper.isEmpty(req.getChannel())){
			logger.error("news type is null");
			resp.setMsg("参数错误，请重试");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
		}
		
		
		if (req.getType() != null) {
			hqlFilter.addEQFilter("type", req.getType());
		}

		if (StringHelper.isNotEmpty(req.getChannel())) {
			hqlFilter.addLKFilter("channel", req.getChannel());
		}
		
		hqlFilter.addSort("cts");
		hqlFilter.addOrder("desc");
		
		List<HKNews> totallist = new ArrayList<HKNews>();
		totallist = newsService.findByFilter(hqlFilter);
		
		newslist = newsService.findByFilter(hqlFilter, req.getCurPage(),
				req.getPageSize());
		
		for(HKNews news :newslist){
			
			HKNewsRes newsRes = new HKNewsRes();
			ReqToModelHelper.copyReqValueToModel(news, newsRes);
			
			newsRes.setCtsStr(DateUtil.longToShortDate(news.getCts()));
			newsRes.setCtsStr(DateUtil.longToLongDate(news.getCts()));
			newsReslist.add(newsRes);
		
		}

		resp.setResult(newsReslist);
		resp.setSize(totallist.size());
		resp.setResultCode(StatusCode.SUCCESS.key());

		return resp;
	}
	
	
	/**
	 * 根据Id获取新闻
	 * 
	 * @author jeff
	 * @return
	 */
	@RequestMapping(value = "/getById", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<HKNewsRes> getById(@RequestBody HKNewsReq req) {
		
		
		ResponseBase<HKNewsRes> resp = new ResponseBase<HKNewsRes>();
		
		logger.info("news getById id ={}", req.getId());
		
		if(req.getId() == null){
			resp.setMsg("参数错误，请重试");
			resp.setResultCode(StatusCode.PARAM_ERROR.key());
		}
		
		HKNews news = newsService.findById(req.getId());
		
		if(news == null){
			resp.setMsg("新闻不存在，请重试");
			resp.setResultCode(StatusCode.DATA_NOTEXISTS.key());
		}
		 
			
		HKNewsRes newsRes = new HKNewsRes();
		
		ReqToModelHelper.copyReqValueToModel(news, newsRes);
		if (news.getContent()  != null) {
			String contentTxt;
			try {
				contentTxt = new String(news.getContent(), "UTF-8");
				newsRes.setContentStr(contentTxt);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		newsRes.setCtsStr(DateUtil.longToShortDate(news.getCts()));
		newsRes.setCtsStr(DateUtil.longToLongDate(news.getCts()));
		
		resp.setResult(newsRes); 
		resp.setResultCode(StatusCode.SUCCESS.key());
		
		return resp;
	}
}
